﻿using System.Threading.Tasks;

namespace Client.Panels
{
    public class BKRankingPanel : BasePanel
    {
        protected override string[] parentsName { get => new string[] { "BasePanel" }; }

        DataCenter.Models.BKRanking[] bks;
        DataCenter.Enums.BKType bkType;
        int bkNum => DataCenter.DataCenter.bkTotal;
        int pageNum
        {
            get
            {
                if (bkNum % 20 == 0)
                    return bkNum / 20;
                return bkNum / 20 + 1;
            }
        }
        int numPerPage = 20; //服务器一次最多只返回20条数据
        int currentPage = 1;
        int desc = 0;

        bool canChangePage = true;
        int limitChangePageMS = 2000;

        public BKRankingPanel(DataCenter.Enums.BKType bkType, string clientText):base(clientText)
        {
            this.bkType = bkType;
            Managers.Manager.panelM.ClearPanels();
            FillFields<DataCenter.Models.BKRanking>(panelName);
        }

        protected override async void UpdatePanel()
        {
            await UpdatePanelAsync();
        }
        async Task<int> UpdatePanelAsync()
        {
            try
            {
                bks = await DataCenter.DataCenter.GetBKRanking(bkType, currentPage, desc);
                UpdatePanel(bks);
                return 0;
            }
            catch (DataCenter.MyException.APIReturnException e)
            {
                var l = e.Message.Split('~');
                if (l[0] == "1000")
                    Utility.ShowLoginAgain();
                throw;
            }
        }


        public override void PageDown()
        {
            if (canChangePage && currentPage < pageNum)
            {
                currentPage++;
                Managers.Manager.cronM.StartCron(true);
                InitIndex();
                LimitChangePage();
            }
        }
        public override void PageUp()
        {
            System.Console.WriteLine(bkNum);
            if (canChangePage && currentPage > 1)
            {
                currentPage--;
                Managers.Manager.cronM.StartCron(true);
                InitIndex();
                LimitChangePage();
            }
        }
        public async override void Enter()
        {
            var p = new GGRankingPanel(bks[y].symbol, bks[y].name);
            await p.Init();
            Managers.Manager.panelM.AddPanel(p);
        }

        public override void OnAwake()
        {
            base.OnAwake();
            Managers.Manager.cronM.updateSuccessfully += UpdatePanel;
            Managers.Manager.cronM.StartCron(true);
            Managers.Manager.cronM.ClearAll();
        }
        public override void OnForeground()
        {
            base.OnForeground();
            Managers.Manager.hotStockInfo = null;
        }
        public override void OnSleep()
        {
            base.OnSleep();
            Managers.Manager.cronM.updateSuccessfully -= UpdatePanel;
        }
        protected override void ClearCron() { }
        protected override void UpdateHotStockInfo(){ }

        async void LimitChangePage()
        {
            canChangePage = false;
            await Task.Delay(limitChangePageMS);
            canChangePage = true;
        }

        protected override string GetYPositionString()
        {
            int i = (currentPage - 1) * numPerPage + y + 1;
            var str = bkNum + "之" + i + "项";
            return str;
        }
    }
}
